package network;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Mariam
 */
public class ThreadSendMsg extends Thread {

    private DatagramSocket socket;
    private InetAddress addr;
    private byte[] msg;
    private ChatNI parent;

    /**
     * Constructs a thread that will send a message in UDP
     * @param socket the socket used to send the message
     * @param addr ths IP address of the remote user (receiver of the message)
     * @param msg the message to send
     * @param parent the chatNI which has created the thread
     * 
     */
    public ThreadSendMsg(DatagramSocket socket, InetAddress addr, byte[] msg, ChatNI parent) {
        this.parent = parent;
        this.socket = socket;
        this.addr = addr;
        this.msg = msg;
    }

    @Override
    public void run() {
        try {
            socket.send(new DatagramPacket(msg, msg.length, addr, parent.COMMON_PORT_SEND_UDP));
        } catch (IOException ex) {
            Logger.getLogger(ThreadSendMsg.class.getName()).log(Level.SEVERE, null, ex);
        }

    }
}
